class code4 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();

        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();

        int[] hash1 = new int[26];
        int[] hash2 = new int[26];

        for(char ch:pp) {
            hash2[ch-'a']++;
        }
        int len = p.length();
        int left=0,right=0;
        int count = 0;//有效字符的个数
        while(right < s.length()) {
            //进
            hash1[ss[right]-'a']++;
            if(hash1[ss[right]-'a'] <= hash2[ss[right]-'a']) count++;
            //判断
            if(right-left+1 > len) {
                //出
                if(hash1[ss[left]-'a'] <= hash2[ss[left]-'a']) count--;
                hash1[ss[left++]-'a']--;
            }
            if(len == count) ret.add(left);
            right++;
        }
        return ret;
    }
}